"""
连接服务器的框架设计：需要支持 "POP3"、"IMAP"、"HTTP" 三种协议，可以将这三种协议作为产品类，共同实现一个接口。
"""
from abc import ABC, abstractmethod


class ServerConnectProtocol(ABC):
    @abstractmethod
    def connect(self):
        pass


class ServerConnectPOP3(ServerConnectProtocol):
    def connect(self):
        print('正在以POP3协议连接服务器...')


class ServerConnectIMAP(ServerConnectProtocol):
    def connect(self):
        print('正在以IMAP协议连接服务器...')


class ServerConnectHTTP(ServerConnectProtocol):
    def connect(self):
        print('正在以HTTP协议连接服务器...')


class ServerConnectFactory:
    def create_connection(self, protocol_type):
        if protocol_type is None:
            return None

        if protocol_type.upper() == 'POP3':
            return ServerConnectPOP3()
        elif protocol_type.upper() == 'IMAP':
            return ServerConnectIMAP()
        elif protocol_type.upper() == 'HTTP':
            return ServerConnectHTTP()

        raise Exception('连接协议错误, 请核查...')


if __name__ == '__main__':
    server_connect_factory = ServerConnectFactory()
    server_connect_http = server_connect_factory.create_connection('http')
    server_connect_pop3 = server_connect_factory.create_connection('pop3')
    server_connect_http.connect()
    server_connect_pop3.connect()
